#include <bits/stdc++.h>
using namespace std;
int n, m, tt, a[100005], b[100005], c[100005];
int t, d[100005], x[15], ok[505];
char c1[22][100005];
signed main() {
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> b[i];
    }
    int cur = 1, mx = 0;
    while (cur < n) {
        cur *= 2;
        mx++;
    }
    //cout << mx << "\n";
    //return 0;
    for (int i = 1; i <= mx; i++) {
        for (int j = 1; j <= (1 << mx - i); i++) {
            cin >> c1[i][j];
        }
    }
    cin >> tt;
    while (tt--) {
        for (int i = 0; i < 4; i++) {
            cin >> x[i];
        }
        for (int i = 1; i <= n; i++) {
            c[i] = a[i] ^ x[i % 4];
        }
        int currr = 0;
        for (int i = 1; i <= m; i++) {
            //cin >> t;
            int curr = 1, mxr = 0;
            while (curr < c[i]) {
                curr *= 2;
                mxr++;
            }
            currr ^= i * curr * (curr + 1) / 2;
        }
        cout << currr << "\n";
    }
    return 0;
}
